<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <div class="title-box" :style="{marginBottom: `${errorMsg ? '22px' : '56px'}`}">
        <div class="logo"></div>
        <div class="title">吉思管理系统</div>
      </div>
      <div class="error-box" v-if="errorMsg">
        <div class="error-icon" @click="errorMsg = ''"></div>
        <div class="error-msg">{{ errorMsg }}</div>
      </div>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="flase"
          placeholder="账号"
        >
          <img slot="prefix" src="@/assets/new/login_img1.png" alt="" class="input-icon">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="flase"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <img slot="prefix" src="@/assets/new/login_img2.png" alt="" class="input-icon">
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 60%"
          @keyup.enter.native="handleLogin"
        >
          <img slot="prefix" src="@/assets/new/login_img3.png" alt="" class="input-icon">
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%; height: 56px; border-radius: 4px;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      errorMsg: ''
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.errorMsg = ''
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch((err) => {
            this.loading = false;
            let e = String(err)
            let index = e.indexOf(' ')
            this.errorMsg = e.substring(index + 1, e.length)
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/图像 14@3x.png");
  background-size: cover;
}

.login-form {
  width: 560px;
  background: #FFFFFF;
  border-radius: 12px;
  padding: 60px 100px;
  box-sizing: border-box;
  position: relative;
  .error-box {
    // position: absolute;
    // top: 128px;
    // left: 100px;
    // z-index: 20;
    // width: 360px;
    margin-bottom: 20px;
    width: 100%;
    height: 40px;
    background: #FFF8E2;
    border: 1px solid #FABC89;
    border-radius: 4px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .error-icon {
      width: 16px;
      height: 16px;
      background-image: url("~@/assets/new/login_close.png");
      background-size: 100% 100%;
      background-repeat: no-repeat;
      margin-left: 18px;
      margin-right: 20px;
    }
    .error-msg {
      flex: 1;
      font-size: 14px;
      font-weight: 400;
      color: #F7842D;
      line-height: 36px;
    }
  }
  .title-box {
    width: 100%;
    height: fit-content;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 56px;
    .logo {
      width: 62px;
      height: 62px;
      background-image: url("~@/assets/new/login_logo_jsy.png");
      background-size: 100% 100%;
      background-repeat: no-repeat;
    }
    .title {
      font-size: 35px;
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: bold;
      color: #291915;
      line-height: 42px;
      margin-left: 10px;
    }
  }
  .el-input {
    height: 54px;
    ::v-deep input {
      height: 54px;
    }
  }
  ::v-deep .el-input__prefix {
    display: flex;
    align-items: center;
    left: 18px;
  }
  ::v-deep .el-input__inner {
    padding-left: 54px;
    background: #F8F8FA;
    border: 1px solid #D4D4D4;
    border-radius: 4px;
    font-size: 16px;
    font-weight: 400;
    color: #0B0608;
    line-height: 36px;
    &::placeholder {
      font-size: 16px;
      font-weight: 400;
      color: #BAC1C9;
      line-height: 36px;
    }
  }
  .input-icon {
    height: 16px;
    width: 16px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 128px;
  height: 54px;
  border: 1px solid #D4D4D4;
  border-radius: 4px;
  float: right;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  width: 126px;
  height: 52px;
}
</style>
